package com.atguigu.realtime.app;

import com.atguigu.realtime.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by RUI on 2021/6/28 19:54
 *
 * 同时消费多个topic
 */
public abstract class BaseApp2 {
    public abstract void run(StreamExecutionEnvironment env, Map<String, DataStreamSource<String>> mapStream );

    public void init(int port,
                     int p,
                     String ck,
                     String groupId,
                     String topic,
                     String... otherTopics) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);

        // 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/flink-realtime/ck/" + ck);

        env.enableCheckpointing(3000, CheckpointingMode.EXACTLY_ONCE);

        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);
        env.getCheckpointConfig()
            .enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        // 具体的业务
        Map<String, DataStreamSource<String>> streamMap = new HashMap<>();

        DataStreamSource<String> sourceStream = env.addSource(FlinkSourceUtil.getKafkaSource(groupId, topic));
        streamMap.put(topic,sourceStream);

        for (String otherTopic : otherTopics) {
            DataStreamSource<String> sourceStream2 = env.addSource(FlinkSourceUtil.getKafkaSource(groupId, otherTopic));
            streamMap.put(otherTopic,sourceStream2);
        }

//        sourceStream.print();  // 不同的应用有不同的业务
        run(env,streamMap);

        try {
            env.execute(ck);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
